package com.tensquare.base.service;

import com.tensquare.base.dao.LabelDao;
import com.tensquare.base.pojo.Label;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import util.IdWorker;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import java.util.ArrayList;
import java.util.List;

/**
 * @Auther: hjk
 * @Date: 2019/6/18 22:08
 * @Description:
 */
@Service
@Transactional
public class LabelService {

    @Autowired
    private LabelDao labelDao;

    @Autowired
    private IdWorker idWorker;

    public List<Label> findAll(){
        return labelDao.findAll();
    }

    public Label findById(String labelId){
        return labelDao.findById(labelId).get();
    }

    public void save(Label label){
        label.setId(String.valueOf(idWorker.nextId()));
        labelDao.save(label);
    }

    public void update(Label label){
        labelDao.save(label);
    }

    public void deleteById(String labelId){
        labelDao.deleteById(labelId);
    }

    public List<Label> findSearch(Label label) {
        return labelDao.findAll(new Specification<Label>() {
            /**
             *
             * @param root 也就是要把条件封装到哪个对象中，where 类名 = label.getId
             * @param criteriaQuery 封装的都是关键字，比如 group by ,order by 等
             * @param criteriaBuilder 用来封装条件对象，如果直接返回null 说明查询不需要条件
             * @return
             */
            @Override
            public Predicate toPredicate(Root<Label> root, CriteriaQuery<?> criteriaQuery, CriteriaBuilder criteriaBuilder) {
                //new 一个集合来存放所有查询条件
                List<Predicate> list = new ArrayList<>();
                // where label like "%小明%"
                if(!StringUtils.isEmpty(label.getLabelName())){
                    Predicate predicate = criteriaBuilder.like(root.get("labelName").as(String.class), "%" + label.getLabelName() + "%");
                    list.add(predicate);
                }
                // state = "1"
                if(!StringUtils.isEmpty(label.getState())){
                    Predicate predicate = criteriaBuilder.equal(root.get("state").as(String.class), label.getState());
                    list.add(predicate);
                }
                Predicate[] param = new Predicate[list.size()];
                //list to array
                list.toArray(param);
                return criteriaBuilder.and(param);
            }
        });
    }

    public Page<Label> pageQuery(int page, int size, Label label) {
        // 封装分页对象
        Pageable pageable = PageRequest.of(page -1 ,size);
        return labelDao.findAll(new Specification<Label>() {
            /**
             *
             * @param root 也就是要把条件封装到哪个对象中，where 类名 = label.getId
             * @param criteriaQuery 封装的都是关键字，比如 group by ,order by 等
             * @param criteriaBuilder 用来封装条件对象，如果直接返回null 说明查询不需要条件
             * @return
             */
            @Override
            public Predicate toPredicate(Root<Label> root, CriteriaQuery<?> criteriaQuery, CriteriaBuilder criteriaBuilder) {
                //new 一个集合来存放所有查询条件
                List<Predicate> list = new ArrayList<>();
                // where label like "%小明%"
                if(!StringUtils.isEmpty(label.getLabelName())){
                    Predicate predicate = criteriaBuilder.like(root.get("labelName").as(String.class), "%" + label.getLabelName() + "%");
                    list.add(predicate);
                }
                // state = "1"
                if(!StringUtils.isEmpty(label.getState())){
                    Predicate predicate = criteriaBuilder.equal(root.get("state").as(String.class), label.getState());
                    list.add(predicate);
                }
                Predicate[] param = new Predicate[list.size()];
                //list to array
                list.toArray(param);
                return criteriaBuilder.and(param);
            }
        },pageable);
    }
}
